package com.qingxin.park.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.DetectionData;
import com.qingxin.park.service.BatchProductsService;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.DetectionDataService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;
/**
 * 速测数据
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/detection")
public class DetectionDataAction extends BaseAction{
	@Autowired
	private DetectionDataService detectionDataService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private BatchProductsService batchProductsService;
	
	
	/**
	 * 产品列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model,String pdttype, HttpServletRequest request){
		model.put("pdttype", pdttype);
		return "/trace/detection/detection_list";
	}
	
	
	/**
	 * 企业树形数据
	 * @param pdatatype
	 * @param pdttype
	 * @param pid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/qiyejstree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object cropsjgjstree(String pdatatype,String pdttype, String pid, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<TreeNode> clist = companyService.getCompanyData(loginUser);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "COMP", null, true));
			}
			return list;
		}else if("COMP".equals(pdatatype)){
			//String companyid = null;
			List<TreeNode> clist = farmParkService.getUITreeDataCompany(pid, pdttype, loginUser, false);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "PARK", null, true));
			}
			return list;
		}else if("PARK".equals(pdatatype)){
			List<TreeNode> clist = parkBlockService.getUITreeDataType(pid, pdttype,false, loginUser);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "BLOCK", null, false));
			}
			return list;
		}
		return new ArrayList<Object>();
	}
	/**
	 * 列表数据
	 * @param leftdata
	 * @param datatype
	 * @param pdttype
	 * @param pbn
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String leftdata, String datatype,String pdttype, String pbn, @DateTimeFormat(pattern="yyyy-MM-dd")Date begin,
			@DateTimeFormat(pattern="yyyy-MM-dd")Date end, PageParam pageParam, HttpServletRequest request){
		String companyid = null;
		String parkid = null;
		String blockid = null;
		if("COMP".equals(datatype)){
			companyid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_PARK.equals(datatype)){
			parkid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_BLOCK.equals(datatype)){
			blockid = leftdata;
		}else{
			return new Pager();
		}
		Pager pager = batchProductsService.list(companyid,parkid, blockid,pdttype, pbn, begin, end, pageParam, getLoginUser(request));
		return pager;
	}
	
	/**
	 * 
	 * @param model
	 * @param pdttype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/detection.do", method=RequestMethod.GET)
	public String detection(ModelMap model,String pdtid, HttpServletRequest request){
		model.put("pdtid", pdtid);
		return "/trace/detection/detectionData_list";
	}
	
	/**
	 * 速测数据 信息列表
	 * @param leftdata
	 * @param datatype
	 * @param pdttype
	 * @param pbn
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdataDetection.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdataDetection(String pdtid, String search,PageParam pageParam, HttpServletRequest request){
		Pager pager = detectionDataService.list(pdtid,search, pageParam, getLoginUser(request));
		return pager;
	}
	
	
	/**
	 * 修改（依据标准 处理状态 处理结果 ）
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model,String id, HttpServletRequest request){
		DetectionData data = detectionDataService.get(id);
		model.put("data", data);
		return "/trace/detection/detectionData_edit";
	}
	
	/**
	 * 修改状态
	 * @param data
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateDetection.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid DetectionData data, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = detectionDataService.updateStatus(data.getId(),data.getTestStandard(),data.getTestStatus(),data.getTestResult());
		return info;
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request){
		if(id==null || id.length==0){
			return new SysError("没有选择任何信息.");
		}
		return detectionDataService.delete(id,request);
	}
	
	/**
	 * 查看详情
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/show.do", method=RequestMethod.GET)
	public String show(ModelMap model,String id, HttpServletRequest request){
		DetectionData data = detectionDataService.get(id);
		model.put("data", data);
		return "/trace/detection/detectionData_show";
	}
	
}
